package com.ybu.oodd.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ybu.oodd.mapper.AdminMapper;
import com.ybu.oodd.pojo.*;
import com.ybu.oodd.service.AdminService;
import com.ybu.oodd.utils.excel.ExcelUtil;
import com.ybu.oodd.utils.redis.RedisUtil;
import com.ybu.oodd.utils.time.ScheduledTaskService;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private ExcelUtil excelUtil;
    @Autowired
    private ScheduledTaskService scheduledTaskService;

    /*
     * 根据id查询job
     * */
    @Override
    public Job getJobById(Integer id) {
        return adminMapper.getJobById(id);
    }

    /*
     * 根据id修改job
     * */
    @Override
    public void updateJobById(Job job) {
        adminMapper.updateJobById(job);
    }

    /*
     * 根据ids删除jobs
     * */
    @Override
    public void deleteJob(List<Integer> ids) {
        adminMapper.deleteJob(ids);
    }

    /*
     * 批量新增jobs
     * */
    @Override
    public void saveJobs(List<Job> list) {
        adminMapper.saveJobs(list);
    }

    /*
     * 解析Excel
     * */
    @Override
    public Result parseJobsFromExcel(MultipartFile file) {
        if (file.isEmpty()) {
            return Result.error("文件为空");
        }

        if (!file.getOriginalFilename().toLowerCase().endsWith(".xlsx")) {
            return Result.error("非excel文件");
        }

        try {
            List<Job> jobs = excelUtil.parseJobsFromExcel(file);
            return Result.success("解析成功",jobs);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("解析失败");
        }
    }

    /*
     * 开关vip，修改freeNum
     * 根据userId  修改jobVipEndTime, freeNum
     * */
    @Override
    public void updateVip(User user) {
        adminMapper.updateVip(user);
        //刷新redis
        //
        //
        //
        //
        scheduledTaskService.Vip();
    }

    /*
     * 查询未解决的用户问题
     * */
    @Override
    public PageResult<Problem> getUserProblem(Integer page,Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<Problem> rows = adminMapper.getUserProblem();
        Page<Problem> p = (Page<Problem>) rows;
        return new PageResult<Problem>(p.getTotal(),p.getResult());
    }

    /*
     * 处理用户问题
     * */
    @Override
    public void updateUserProblem(Problem problem) {
        problem.setEndTime(LocalDateTime.now());
        adminMapper.updateUserProblem(problem);
    }

    /*
     * 根据id和user_id 查询url
     * */
    @Override
    public List<Pictures> getPictureUrl(List<Integer> ids, Integer userId) {
        return adminMapper.getPictureUrl(ids,userId);
    }

    /*
     * 分页查询用户
     * */
    @Override
    public PageResult<User> getUser(Integer page, Integer pageSize, Integer id) {
        PageHelper.startPage(page,pageSize);
        List<User> rows = adminMapper.getUser(id);
        Page<User> p = (Page<User>) rows;
        return new PageResult<User>(p.getTotal(),p.getResult());
    }

    /*
     * 查询关键字
     * */
    @Override
    public List<Key> getKey() {
        return adminMapper.getKey();
    }

    /*
     * 修改关键字
     * */
    @Override
    public void updateKey(Key key) {
        if(key.getType().equals("home_title"))
            redisUtil.delete("homeTitle");
        if(key.getType().equals("home_text"))
            redisUtil.delete("homeText");
        adminMapper.updateKey(key);
    }

    /*
     * 新增关键字
     * */
    @Override
    public void saveKey(Key key) {
        adminMapper.saveKey(key);
    }

    /*
     * 删除关键字
     * */
    @Override
    public void deleteKey(Integer id) {
        adminMapper.deleteKey(id);
    }
}
